<?php
if(!defined('DCS_ROOT')) {exit('Access Denied');}

class MemberModel {
	var $DB;
	function MemberModel ( $DB ){
		$this->DB = $DB;
	}	
	function getMember($uid){	
		$sql = "SELECT * FROM member WHERE uid = '$uid' ";
		//de($sql);
		$this->DB->query($sql);
		return $this->DB->fetchRow();	
	}

	function getList($condition){
		$sql = "SELECT * FROM member ";
		$tmp = "";
		if (!empty($condition['condition'])) {
			$tmp .= "WHERE {$condition['condition']} ";
		} 
		if (!empty($condition['order'])) {
			$tmp .= " ORDER BY {$condition['order']} DESC ";
		} 
		if (!empty($condition['page']) && !empty($condition['rows'])) {
			$start = ($condition['page'] - 1) * $condition['rows'];
			$tmp .= "LIMIT " . $start . ", " . $condition['rows'];
		} 
		$sql .= $tmp;
		//de($sql);
		$this->DB->query($sql);
		return $this->DB->fetchAll();
	}
	
	function total($condition) {
		$sql = "SELECT count(*) AS total FROM member ";
		$tmp = "";
		if (!empty($condition['condition'])) {
			$tmp .= "WHERE {$condition['condition']} ";
		} 
		if (!empty($condition['order'])) {
			$tmp .= " ORDER BY {$condition['order']} DESC ";
		} 
		$sql .= $tmp; 
		$this->DB->query($sql);
		$this->DB->fetchRow();
		return $this->DB->getValue("total");
	}
	
	function addOne($val){
		$sql = "INSERT INTO member (";
		foreach($val as $k => $v ){
			$tmp .= "$k,";
			$value .= " '$v',";
		}
		$sql .= rtrim($tmp,",").')  VALUES ('.rtrim($value,",").')';		
		$this->DB->query($sql);
		return $this->DB->affectedRows();
	}
	function editOne($id,$username,$password,$email,$lockuser,$avatars){
		$sql = "UPDATE member SET password='$password',username='$username',email ='$email',lockuser='$lockuser',avatars='$avatars' WHERE uid = '$id' ";
		$this->DB->query($sql);
		return $this->DB->affectedRows();
	}
	
	function delOne($id) {
		$sql = "DELETE FROM member WHERE uid = '$id'" ;
		$this->DB->query($sql);
		return $this->DB->affectedRows();
	}

}
?>
